package com.qfmy.mystation.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.qfmy.mystation.api.APIService;
import com.qfmy.mystation.bean.CategoryBean;
import com.qfmy.mystation.databinding.FragmentLiveWallpaperBinding;
import com.qfmy.mystation.ui.adapter.WallpaperTabAdapter;
import com.qfmy.mystation.utils.Constants;
import com.qfmy.mystation.utils.RxSchedulers;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LiveWallpaperFragment extends Fragment {
    private FragmentLiveWallpaperBinding binding;
    private CompositeDisposable mSubscriptions;
    private List<CategoryBean> menu;
    private int position;

    public static LiveWallpaperFragment newInstance() {
        return new LiveWallpaperFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentLiveWallpaperBinding.inflate(inflater, container, false);
        mSubscriptions = new CompositeDisposable();
        binding.tbVp.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                binding.vpTb.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        binding.vpTb.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                binding.tbVp.selectTab(binding.tbVp.getTabAt(position));
            }
        });
        if (menu == null) {
            getCategories();
        } else {
            binding.vpTb.setAdapter(new WallpaperTabAdapter(requireActivity(), menu, Constants.LIVE_WALLPAPER));
            for (CategoryBean bean :
                    menu) {
                binding.tbVp.addTab(binding.tbVp.newTab().setText(bean.getName()));
            }
        }
        binding.vpTb.setCurrentItem(position);
        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        position = binding.tbVp.getSelectedTabPosition();
        binding = null;
    }

    private void getCategories() {
        Disposable subscribe = APIService.getInstance().apis.getLiveCategories()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(data -> {
                    if (data != null && data.getCategories() != null && data.getCategories().size() > 0) {
                        menu = data.getCategories();
                        binding.vpTb.setAdapter(new WallpaperTabAdapter(requireActivity(), menu, Constants.LIVE_WALLPAPER));
                        for (CategoryBean bean :
                                menu) {
                            binding.tbVp.addTab(binding.tbVp.newTab().setText(bean.getName()));
                        }
                    }
                }, RxSchedulers::processRequestException);
        mSubscriptions.add(subscribe);
    }
}